package com.pairuan.common.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SaTokenConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册Sa-Token拦截器
        registry.addInterceptor(new SaInterceptor(handle -> {
            // 指定需要登录认证的接口
            SaRouter.match("/**")
                    // 排除登录接口、Knife4j接口等不需要认证的接口
                    .notMatch("/login", "/register")
                    .notMatch("/doc.html", "/webjars/**", "/v3/api-docs/**")
                    .notMatch("/favicon.ico", "/error")
                    // 进行权限验证
                    .check(r -> StpUtil.checkLogin());
        })).addPathPatterns("/**");
    }
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 添加静态资源映射
        registry.addResourceHandler("/favicon.ico")
                .addResourceLocations("classpath:/static/");
    }
} 